home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Resources / Online / Term / Extras / Source / term-source.lha / SortCompare.c < prev    next >
C/C++ Source or Header  |  1996-10-20  |  2KB  |  144 lines

  1. /*
  2. **    SortCompare.c
  3. **
  4. **    Phonebook sort comparison routines
  5. **
  6. **    Copyright © 1990-1996 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. **
  9. **    :ts=4
  10. */
  11.  
  12. #ifndef _GLOBAL_H
  13. #include "Global.h"
  14. #endif
  15.  
  16. STATIC LONG
  17. ComparePhone(PhoneEntry **A,PhoneEntry **B)
  18. {
  19.         /* Has entry A been selected? */
  20.  
  21.     if((*A)->Count == -1)
  22.     {
  23.             /* If entry B isn't selected either, compare the
  24.              * names lexically, else entry B is supposed
  25.              * to be `smaller' than entry A.
  26.              */
  27.  
  28.         if((*B)->Count == -1)
  29.             return(Stricmp((*A)->Header->Name,(*B)->Header->Name));
  30.         else
  31.             return(1);
  32.     }
  33.     else
  34.     {
  35.             /* If entry B isn't selected, entry A is supposed
  36.              * to be `smaller' than entry B, else return
  37.              * the difference between both entries.
  38.              */
  39.  
  40.         if((*B)->Count == -1)
  41.             return(-1);
  42.         else
  43.             return((*A)->Count - (*B)->Count);
  44.     }
  45. }
  46.  
  47. STATIC LONG
  48. CompareName(PhoneEntry **A,PhoneEntry **B)
  49. {
  50.     return(Stricmp((*A)->Header->Name,(*B)->Header->Name));
  51. }
  52.  
  53. STATIC LONG
  54. CompareNumber(PhoneEntry **A,PhoneEntry **B)
  55. {
  56.     return(Stricmp((*A)->Header->Number,(*B)->Header->Number));
  57. }
  58.  
  59. STATIC LONG
  60. CompareComment(PhoneEntry **A,PhoneEntry **B)
  61. {
  62.     return(Stricmp((*A)->Header->Comment,(*B)->Header->Comment));
  63. }
  64.  
  65. /****************************************************************************/
  66.  
  67. STATIC LONG
  68. CompareInversePhone(PhoneEntry **A,PhoneEntry **B)
  69. {
  70.     return(ComparePhone(B,A));
  71. }
  72.  
  73. STATIC LONG
  74. CompareInverseName(PhoneEntry **A,PhoneEntry **B)
  75. {
  76.     return(CompareName(B,A));
  77. }
  78.  
  79. STATIC LONG
  80. CompareInverseNumber(PhoneEntry **A,PhoneEntry **B)
  81. {
  82.     return(CompareNumber(B,A));
  83. }
  84.  
  85. STATIC LONG
  86. CompareInverseComment(PhoneEntry **A,PhoneEntry **B)
  87. {
  88.     return(CompareComment(B,A));
  89. }
  90.  
  91. /****************************************************************************/
  92.  
  93. SORTFUNC
  94. GetSortFunc(LONG How,BOOL ReverseOrder)
  95. {
  96.     SORTFUNC SortFunc;
  97.  
  98.     switch(How)
  99.     {
  100.         case SORT_NAME:
  101.  
  102.             if(ReverseOrder)
  103.                 SortFunc = (SORTFUNC)CompareInverseName;
  104.             else
  105.                 SortFunc = (SORTFUNC)CompareName;
  106.  
  107.             break;
  108.  
  109.         case SORT_COMMENT:
  110.  
  111.             if(ReverseOrder)
  112.                 SortFunc = (SORTFUNC)CompareInverseComment;
  113.             else
  114.                 SortFunc = (SORTFUNC)CompareComment;
  115.  
  116.             break;
  117.  
  118.         case SORT_NUMBER:
  119.  
  120.             if(ReverseOrder)
  121.                 SortFunc = (SORTFUNC)CompareInverseNumber;
  122.             else
  123.                 SortFunc = (SORTFUNC)CompareNumber;
  124.  
  125.             break;
  126.  
  127.         case SORT_SELECTION:
  128.  
  129.             if(ReverseOrder)
  130.                 SortFunc = (SORTFUNC)CompareInversePhone;
  131.             else
  132.                 SortFunc = (SORTFUNC)ComparePhone;
  133.  
  134.             break;
  135.  
  136.         default:
  137.  
  138.             SortFunc = (SORTFUNC)CompareName;
  139.             break;
  140.     }
  141.  
  142.     return(SortFunc);
  143. }
  144.